/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dhfwk_publisher_test.h"

#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>

using OHOS::DistributedHardware::DHTopic;
using OHOS::DistributedHardware::DHFWKPublisherTest;
using OHOS::DistributedHardware::Role;

std::map<DHTopic, std::string> TOPIC_MSGS = {
    { DHTopic::TOPIC_START_DSCREEN, "Start DScreen" },
    { DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, "Sink Project Window" },
    { DHTopic::TOPIC_STOP_DSCREEN, "Stop DScreen" },
    { DHTopic::TOPIC_DEV_OFFLINE, "Dev Offline" }
};

const std::vector<int> VALID_CMDS = { 0, 1, 2, 11, 12, 13, 14, 21, 22, 23, 24, 25, 26, 27, 28, 30, 100 };

bool IsCmdValid(int cmd)
{
    if (std::find(VALID_CMDS.begin(), VALID_CMDS.end(), cmd) == VALID_CMDS.end()) {
        return false;
    }

    return true;
}

void Help()
{
    std::cout << "*************************************************" << std::endl << std::endl;
    std::cout << "dhfwk_publisher_test help: " << std::endl;
    std::cout << "0: Help" << std::endl;
    std::cout << "100: Exit" << std::endl;

    std::cout << "1: Init As Publisher" << std::endl;
    std::cout << "2: Init As Listener" << std::endl;

    std::cout << "11: Publish TOPIC_START_DSCREEN Msg" << std::endl;
    std::cout << "12: Publish TOPIC_SINK_PROJECT_WINDOW_INFO Msg" << std::endl;
    std::cout << "13: Publish TOPIC_STOP_DSCREEN Msg" << std::endl;
    std::cout << "14: Publish TOPIC_DEV_OFFLINE Msg" << std::endl;

    std::cout << "21: Register TOPIC_START_DSCREEN Listener" << std::endl;
    std::cout << "22: Register TOPIC_SINK_PROJECT_WINDOW_INFO Listener" << std::endl;
    std::cout << "23: Register TOPIC_STOP_DSCREEN Listener" << std::endl;
    std::cout << "24: Register TOPIC_DEV_OFFLINE Listener" << std::endl;

    std::cout << "25: Unregister TOPIC_START_DSCREEN Listener" << std::endl;
    std::cout << "26: Unregister TOPIC_SINK_PROJECT_WINDOW_INFO Listener" << std::endl;
    std::cout << "27: Unregister TOPIC_STOP_DSCREEN Listener" << std::endl;
    std::cout << "28: Unregister TOPIC_DEV_OFFLINE Listener" << std::endl;

    std::cout << "30: Publish Random Topic Msg" << std::endl;
    std::cout << "*************************************************" << std::endl << std::endl;
}

int main()
{
    std::shared_ptr<DHFWKPublisherTest> testPtr = nullptr;
    Help();
    std::cout << "Please choose Role: 1 for Publisher, 2 for Listener" << std::endl;
    int32_t in;
    std::cin >> in;
    if (in != 1 && in !=2) {
        std::cout << "Init Role failed" << std::endl;
        return -1;
    }

    if (in == 1) {
        testPtr = std::make_shared<DHFWKPublisherTest>(Role::PUBLISHER);
    } else {
        testPtr = std::make_shared<DHFWKPublisherTest>(Role::LISTENER);
    }

    while (true) {
        std::cout << std::endl;
        std::cout << "Enter the Command: ";
        std::cin >> in;
        if (!IsCmdValid(in)) {
            std::cout << "Input Invalid, input again!" << std::endl;
            continue;
        }
        
        if (in == 100) {
            std::cout << "Exit!" << std::endl;
            break;
        }

        switch (in) {
            case 0: {
                Help();
                break;
            }
            case 1:
            case 2: {
                std::cout << "Already Init, continue..." << std::endl;
                break;
            }
            case 11: {
                testPtr->PublishMessage(DHTopic::TOPIC_START_DSCREEN, TOPIC_MSGS[DHTopic::TOPIC_START_DSCREEN]);
                break;
            }
            case 12: {
                testPtr->PublishMessage(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, TOPIC_MSGS[DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO]);
                break;
            }
            case 13: {
                testPtr->PublishMessage(DHTopic::TOPIC_STOP_DSCREEN, TOPIC_MSGS[DHTopic::TOPIC_STOP_DSCREEN]);
                break;
            }
            case 14: {
                testPtr->PublishMessage(DHTopic::TOPIC_DEV_OFFLINE, TOPIC_MSGS[DHTopic::TOPIC_DEV_OFFLINE]);
                break;
            }
            case 21: {
                testPtr->RegisterListener(DHTopic::TOPIC_START_DSCREEN);
                break;
            }
            case 22: {
                testPtr->RegisterListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO);
                break;
            }
            case 23: {
                testPtr->RegisterListener(DHTopic::TOPIC_STOP_DSCREEN);
                break;
            }
            case 24: {
                testPtr->RegisterListener(DHTopic::TOPIC_DEV_OFFLINE);
                break;
            }
            case 25: {
                testPtr->UnregisterListener(DHTopic::TOPIC_START_DSCREEN);
                break;
            }
            case 26: {
                testPtr->UnregisterListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO);
                break;
            }
            case 27: {
                testPtr->UnregisterListener(DHTopic::TOPIC_STOP_DSCREEN);
                break;
            }
            case 28: {
                testPtr->UnregisterListener(DHTopic::TOPIC_DEV_OFFLINE);
                break;
            }
            case 30: {
                testPtr->PublishRandomMessage();
            }
        }
    }

    return 0;
}